<template>
  <a-modal
    :title='title'
    :width='800'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>

    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model' :rules='validatorRules'>
        <a-form-model-item label='栏目名称' required prop='columnName'>
          <a-input placeholder='请输入栏目名称' v-model='model.columnName' />
        </a-form-model-item>
        <a-form-model-item label='栏目编码' required prop='columnCode'>
          <a-input placeholder='请输入栏目编码' v-model='model.columnCode' />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@api/manage'

export default {
  name: 'hCmsColumnModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        columnName: [{ required: true, message: '请输入栏目名称!' }],
        columnCode: [{ required: true, message: '请输入栏目编码!' }]
      },
      url: {
        add: '/h/cms/column',
        edit: '/h/cms/column'
      }
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }


  }
}
</script>

<style scoped>

</style>